#include "sfsis.h"
#include "debug.h"
su8 i_isTimerEnable = SF_False;
su16 i_timeStamp_ms = 0;
SF_Boolen sf_cmd_task(void);
void sf_timerDo(void)
{
    if (i_isTimerEnable == SF_True)
    {
        i_timeStamp_ms++;
    }
}

void sfinit(void)
{
    hwl_init();
    config_init();
    mwl_rs_init();
    mwl_dev_init();
    bsf_init(); //属于mwllayer
    mwl_rs_init();
    i_isTimerEnable = SF_True;
    sf_onstart_action();
}

typedef enum
{
    SFSS_EFFECTOR_UPDATE,
    SFSS_UPDATE_RAM,
    SFSS_FLUSHING_LED,
} SF_Scan_Stage;

void sfmain(void)
{
    static SF_Boolen isPress = SF_False;
    static su16 adcLongPressDetect = 0;
    static SF_Scan_Stage stage = SFSS_EFFECTOR_UPDATE;
    static su8 memState = 0xFF;
    static su8 curState = 0;
    static su8 memPairState = 0xFF;
    static su8 curPairState = 0;
    static su8 hisHostState = 0;
    static su8 curHostState = 0;
    bsf_do();
    mwl_dev_update();
    if (gCfgFirmwarePtr->adcVal <240 && isPress == SF_False)
    {
            sf_key_on_press_action();
            isPress = SF_True;
    }
    else if (gCfgFirmwarePtr->adcVal > 240 && isPress == SF_True)
    {
            sf_key_on_release_action();
            adcLongPressDetect = 0;
            isPress = SF_False;
    }
    

    curPairState = bsf_isPaired();
    if (curPairState == 0)
    {
        if (memPairState != curPairState)
        {
            sf_notpair_action();
            memPairState = curPairState;
        }
    }
    else
    {
        curState = bsf_isLinkEstablished();
        if (curState)
        {
            if (memState != curState)
            {
                sf_online_edge_action();
                memState = curState;
            }
            if (sf_cmd_task() == SF_True)
            {
                sf_cmd_action();
            }
            sf_online_task();
        }
        else
        {
            if (memState != curState)
            {
                sf_offline_edge_action();
                memState = curState;
            }
            sf_offline_task();
        }
    }
    curHostState = bsf_isAlive();
    if(curHostState != hisHostState)
    {
        hisHostState = curHostState;
        if(curHostState !=0)
        {
            sf_on_host_alive_action();
        }
        else
        {
            sf_on_host_dead_action();
        }
    }
    if(curHostState)
    {
        sf_host_alive_task();
    }
    else
    {
        sf_host_dead_task();
    }
    



    sf_common_task();

    if (i_timeStamp_ms >= (SCAN_GAP_MS / 3))
    {
        if (isPress == SF_True)
        {

            if (adcLongPressDetect > 100)
            {
                //长按1秒
                sf_key_on_long_press_task();
            }
            else
            {
                adcLongPressDetect++;
            }
        }
        switch (stage)
        {
        case SFSS_EFFECTOR_UPDATE:
            effector_task();
            stage = SFSS_UPDATE_RAM;
            break;
        case SFSS_UPDATE_RAM:
            mwl_rs_update();
            stage = SFSS_FLUSHING_LED;
            break;
        case SFSS_FLUSHING_LED:
            mwl_rs_flush();
            stage = SFSS_EFFECTOR_UPDATE;
            break;
        default:
            break;
        }
        i_isTimerEnable = SF_False;
        i_timeStamp_ms = 0;
        i_isTimerEnable = SF_True;
    }
}
